﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

namespace Z1029.PhiPlayer.OpenSource
{
    public class OverUI : MonoBehaviour
    {
        [Tooltip("Texts Set")]
        public Text levelName, levelHarder, score, lastScore, increaseScore, accurate, combo, perfect, good, bad, miss;
        public Text rsk;
        public Text playerName;
        public Image playerColor;

        public GameObject fullCombo, allPerfect;

        public Image background, backgroundBlur, backgroundBig;

        [Tooltip("RankScore")]
        public GameObject phi, v, s, a, b, c, f;

        public Animator overUiAnim;

        public static string level, harder;
        public static Sprite illustration, illustrationBlur;
        public static int newScore, maxCombo, perfectCount, goodCount, badCount, missCount;
        public static float rankHarder;


        private void Start()
        {
            playerName.text = PlayerPrefs.GetString("PlayerName", "Guest,#FF0000");
            lastScore.text = "0000000";
            int deltaScore = (newScore);
            increaseScore.text = deltaScore > 0 ? "+" + deltaScore : deltaScore.ToString();
            if (playerName.text.Contains(",") && playerName.text.Contains("#"))
            {
                Color c;
                if (ColorUtility.TryParseHtmlString(playerName.text.Split(',')[1], out c))
                {
                    playerColor.color = c;
                    playerName.text = playerName.text.Split(',')[0];
                }
                else
                {
                    playerColor.color = Color.blue;
                }
            }
            levelName.text = level;
            levelHarder.text = harder;
            score.text = newScore.ToString("0000000");

            combo.text = maxCombo.ToString();
            background.sprite = illustration;
            backgroundBlur.sprite = illustrationBlur;
            backgroundBig.sprite = illustrationBlur;
            backgroundBlur.color = new Color(1, 1, 1, PlayerPrefs.GetFloat("BackgroundBrightness", 0.5f));

            int noteCount = perfectCount + goodCount + badCount + missCount;
            float acc = (perfectCount + goodCount * 0.6f) / noteCount;

            accurate.text = string.Format("{0:0.00}%", acc * 100f);
            if (noteCount == perfectCount)
            {
                allPerfect.SetActive(true);
            }
            else if (noteCount == perfectCount + goodCount)
            {
                fullCombo.SetActive(true);
            }
            bad.text = badCount.ToString();
            miss.text = missCount.ToString();
            perfect.text = perfectCount.ToString();
            good.text = goodCount.ToString();
            if (newScore == 1000000)
            {
                phi.SetActive(true);
            }
            else if (newScore >= 960000)
            {
                v.SetActive(true);
            }
            else if (newScore >= 920000)
            {
                s.SetActive(true);
            }
            else if (newScore >= 880000)
            {
                a.SetActive(true);
            }
            else if (newScore >= 820000)
            {
                b.SetActive(true);
            }
            else if (newScore >= 720000)
            {
                c.SetActive(true);
            }
            else f.SetActive(true);
        }

        public void ClickBack()
        {
            StartCoroutine(Back());
        }

        public void ClickRetry()
        {
            StartCoroutine(Replay());
        }

        IEnumerator Replay()
        {
            overUiAnim.SetTrigger("ExitOverMenu");
            yield return new WaitForSeconds(1);
            Application.LoadLevelAsync("GamePlay");
        }

        IEnumerator Back()
        {
            overUiAnim.SetTrigger("ExitOverMenu");
            yield return new WaitForSeconds(1);
            Application.LoadLevelAsync("Menu");
        }
    }

    public class ScoreManager
    {

    }
}